home *** CD-ROM | disk | FTP | other *** search
/ APDL Eductation Resources / APDL Eductation Resources.iso / earthmap / _earthmap / maps / getpar / getpar_dec next >
Encoding:
Text File  |  1989-04-01  |  3.7 KB  |  168 lines

  1. /*
  2.  *
  3.  *  source file:   ./filters/loclib/getpar_decode.c
  4.  *
  5.  * Joe Dellinger (SEP), June 11 1987
  6.  *    Inserted this sample edit history entry.
  7.  *    Please log any further modifications made to this file:
  8.  */
  9.  
  10. /* Revised 3-8-86 stew  Added timestamp to recover older functionality
  11.  *            when presented with multiple tags
  12.  * Revised 9-18-86 joe  Added '1' type ... y or 1 for yes, n or 0 for no
  13.  */
  14. #include <stdio.h>
  15. #include "fastpar.h"
  16.  
  17. /*
  18.  * split off first tag "n1" from input tags "n1 nt ne"
  19.  */
  20. static int tag_split(tag,tlen,subtag,sublen)
  21. register char *tag;
  22. char **subtag;
  23. register int tlen;
  24. int *sublen;
  25. {
  26.  register int i,j;
  27.  
  28.  for(i=0; i<tlen; i++) if(tag[i] != ' ') break;
  29.  if(i == tlen) return(0); /* all bytes consumed */
  30.  *subtag = tag+i;
  31.  for(j=i+1; j<tlen; j++) if(tag[j] == ' ') break;
  32.  *sublen = (j-i);
  33.  return(1);
  34. }
  35.  
  36. /*
  37.  * take string "n1 nt ne" and look up stored parameters with those
  38.  * names, returning value with most recent timestamp
  39.  */
  40. int getpar_decode(q,qlen,tag,type,val)
  41. hash_item **q;
  42. int qlen;
  43. char *tag, *type;
  44. MIXED val;
  45. {
  46.  register char *next, *end;
  47.  char *subtag;
  48.  int sublen, count = 0 ;
  49.  register hash_item *foundit, *saveit;
  50.  extern hash_item *getpar_hash_lookup();
  51.  register int bigtime = -1;
  52.  
  53.  next=tag; end = tag+strlen(tag);
  54.  while(tag_split(next,end-next,&subtag,&sublen)) {
  55.     foundit = getpar_hash_lookup(q,qlen,subtag,sublen);
  56.     if(foundit != ((hash_item *) NULL))
  57.     if(bigtime < foundit->timestamp) {
  58.         bigtime = foundit->timestamp;
  59.         saveit = foundit;
  60.         }
  61.     next = subtag+sublen;
  62.     }
  63.  if(bigtime >= 0) count = getpar_getval(saveit,type,val);
  64.  return(count);
  65.  }
  66.  
  67. /*
  68.  * return 1 if char c is not in string s; else return 0
  69.  */
  70. static int
  71. getpar_neq(c,s)
  72. register int c;
  73. register char *s;
  74. {
  75.     do {
  76.         if(*s == c) {
  77.             return(0); 
  78.         }
  79.     } 
  80.     while(*s++);
  81.     return(1);
  82. }
  83.  
  84. /*
  85.  * take stored string value and convert it according to "type" format
  86.  * result stored at "ptr"
  87.  *
  88.  * type formats:
  89.  *               "i" or "d"  to convert to integer
  90.  *               "r" or "f"  to convert to real
  91.  *               "g"         to convert to double precision
  92.  *               "s"         to keep as a string value
  93.  *
  94.  * stored string values may specify a vector of numerics:
  95.  *
  96.  *               3.0,5x3.5,-1.0,3*2.2
  97.  * 
  98.  * yields the result:
  99.  *
  100.  *               3.0,3.5,3.5,3.5,3.5,3.5,-1.0,2.2,2.2,2.2
  101.  *
  102.  * the function's return value will be the count (10) of items converted
  103.  */
  104. static int getpar_getval(foundit,type,ptr)
  105. hash_item *foundit;
  106. char *type;
  107. MIXED ptr;
  108. {
  109.     register char *sptr, *str;
  110.     register int ival, jval;
  111.     register int index, endindex;
  112.     extern double atof();
  113.     extern int atoi();
  114.     float flt;
  115.     double dubble;
  116.     int integer;
  117.     index=0;
  118.     str = foundit->val;
  119.     ival = foundit->vlen;
  120.  
  121.     while(ival > 0) {
  122.     endindex= index+1;
  123.     if(*type != 's') {
  124.         sptr = str;
  125.         jval = ival;
  126.         while(jval && getpar_neq((int) (*sptr),"*x,"))
  127.             { sptr++; jval--;}
  128.         if(jval > 0)
  129.             if(*sptr=='*' || *sptr=='x') {
  130.             endindex= index+atoi(str);
  131.             str= sptr+1;
  132.             ival = jval-1;
  133.         }
  134.     }
  135.     switch(*type) {
  136.     case 'd':
  137.     case 'i':
  138.         integer= atoi(str);
  139.         while(index<endindex) ptr.i[index++]= integer;
  140.         break;
  141.     case '1':
  142.         if (str[0] == 'y' || str[0] == '1' || str[0] == 'Y')
  143.             integer = 1;
  144.         else
  145.             integer = 0;
  146.         while(index<endindex) ptr.i[index++]= integer;
  147.         break;
  148.     case 'f':
  149.     case 'r':
  150.         flt= atof(str);
  151.         while(index<endindex) ptr.f[index++]= flt;
  152.         break;
  153.     case 'g':
  154.         dubble= atof(str);
  155.         while(index<endindex) ptr.g[index++]= dubble;
  156.         break;
  157.     case 's':
  158.         bcopy(str,ptr.s,ival);
  159.         ptr.s[ival]='\0';
  160.         return(1);
  161.     default:
  162.         err("getpar() unknown conversion type %c\n",*type);
  163.     }
  164.     while((--ival) && ((*(str++)) != ',')); /* skip past next comma */
  165.     }
  166.     return(endindex);
  167. }
  168.